package com.yunkuhui.www.demo.service.impl;

import com.yunkuhui.www.demo.dao.test.TestUserDao;
import com.yunkuhui.www.demo.dao.test1.Test1UserDao;
import com.yunkuhui.www.demo.model.TestUser;
import com.yunkuhui.www.demo.service.TestUserService;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service("testUserService")
@SuppressWarnings("unchecked")
@Transactional(value = "test1DataSourceTransactionManager", isolation = Isolation.READ_COMMITTED,
		propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class TestUserServiceImpl implements TestUserService {

	@Resource
	private TestUserDao testUserDao;

	@Resource
	private Test1UserDao test1UserDao;

	@Resource(name = "redisTemplate0")
	private RedisTemplate redisTemplate;

	@Resource(name = "redisTemplate1")
	private RedisTemplate redisTemplate1;

	@Resource(name = "testTemplate")
	private MongoTemplate testTemplate;

	@Resource(name = "test1Template")
	private MongoTemplate test1Template;

	@Override
	public int removeByPrimaryKey(String id) {
		return 0;
	}

	@Override
	@Transactional(value = "testDataSourceTransactionManager", isolation = Isolation.READ_COMMITTED,
			propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
//	@MultiRollback({"testDataSourceTransactionManager", "test1DataSourceTransactionManager"})
	public Map<String, Object> saveTestUser(TestUser testUser) {
		int n = testUserDao.insert(testUser);
		int count = test1UserDao.insert(testUser);
		redisTemplate.opsForValue().set("user", testUser);
		redisTemplate1.opsForValue().set("user", testUser);
		TestUser user = testTemplate.insert(testUser, "test_user");
		TestUser user1 = test1Template.save(testUser, "test_user");
		HashMap<String, Object> map = new HashMap<>();
		map.put("count1", n);
		map.put("count2", count);
		map.put("user", user);
		map.put("user1", user1);
		return map;
	}

	@Override
	public Map<String, Object> insertSelective(TestUser testUser) {
		return null;
	}

	@Override
	public TestUser queryByPrimaryKey(String id) {
		return null;
	}

	@Override
	public int modifyByPrimaryKeySelective(TestUser testUser) {
		return 0;
	}

	@Override
	public int modifyByPrimaryKey(TestUser testUser) {
		return 0;
	}
}
